package com.skyinno.mrms.station.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.io.filefilter.FalseFileFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.skyinno.mrms.common.model.UploadFile;
import com.skyinno.mrms.common.service.impl.BaseServiceImpl;
import com.skyinno.mrms.station.dao.StationTypeDao;
import com.skyinno.mrms.station.model.FileView;
import com.skyinno.mrms.station.model.MapStationType;
import com.skyinno.mrms.station.model.QueryView;
import com.skyinno.mrms.station.model.View;
import com.skyinno.mrms.station.service.StationTypeService;
import com.skyinno.mrms.utils.Constants;
import com.skyinno.mrms.utils.FrameException;
import com.skyinno.mrms.utils.MessageConstants;

@Service
public class StationTypeServiceImpl extends BaseServiceImpl  implements StationTypeService   {

		protected Logger logger = Logger.getLogger(this.getClass());

		@Autowired
		private StationTypeDao dao;
		
		@SuppressWarnings("unchecked")
		@Override
		public Map<String, Object> queryPage(QueryView view) {
			DetachedCriteria dc = DetachedCriteria.forClass(MapStationType.class)
					.add(Restrictions.eq("flag", Constants.VALID)).addOrder(Order.asc("indexOrder"));
			view.setCount(dao.queryCount(dc));
			ArrayList<View> rows = new ArrayList<View>();
			if (view.getCount() > 0) {
				super.resetStart(view);
				super.setOrder(dc, view);
				List<MapStationType> list = dao.queryPage(dc, view.getPage(), view.getRows());
				for (MapStationType st : list) {
					View v = new View();
					v.setId(st.getId());
					v.setTypeId(st.getTypeId());
					v.setName(st.getName());
					v.setNameEn(st.getNameEn());
					v.setIcon(st.getIcon());
					v.setIndexOrder(st.getIndexOrder());
					v.setRemark(st.getRemark());
					rows.add(v);
				}
			}
			return super.setGridData(view.getCount(), rows);
		}

		/**
		 * 逻辑删除站点类型
		 * 
		 * @param ids
		 * @return
		 * @throws FrameException
		 */
		@Override
		@Transactional(readOnly=false)
		public String remove(String ids) throws FrameException {
			if (StringUtils.isNotBlank(ids)) {
				String[] id = ids.split(",");
				for (String s : id) {
					MapStationType mapStationType = this.checkMapStationType(s);
					mapStationType.setFlag(Constants.INVALID);
					dao.update(mapStationType);
				}
			}
			return MessageConstants.REMOVE_SUCCESS;
		}

		/**
		 * 主键查询站点类型
		 * 
		 * @param id
		 * @return
		 * @throws FrameException
		 */
		@Override
		public MapStationType checkMapStationType(String id) throws FrameException {
			MapStationType model = dao.queryEntityById(MapStationType.class, id);
			return model;
		}

		@Override
		@Transactional(readOnly=false)
		public String add(View view, Locale locale) throws FrameException {
			//type_id不能重复
			DetachedCriteria dc = DetachedCriteria.forClass(MapStationType.class)
					.add(Restrictions.eq("typeId", view.getTypeId()))
					.add(Restrictions.eq("flag", Constants.VALID));
			if (dao.queryCount(dc) > 0) {
				throw new FrameException(super.message.getMessage(
	                    "站点类型重复", locale));
			}
			MapStationType model = new MapStationType();
			model.setFlag(Constants.VALID);
			dao.save(this.createModel(view, model));
			return MessageConstants.ADD_SUCCESS;
		}

		@Override
		public MapStationType createModel(View view, MapStationType model) throws FrameException {
			model.setFlag(Constants.VALID);
			model.setIcon(view.getIcon());
			model.setIndexOrder(view.getIndexOrder());
			model.setName(view.getName());
			model.setNameEn(view.getNameEn());
			model.setRemark(view.getRemark());
			model.setTypeId(view.getTypeId());
			return model;
		}

		@Override
		public Map<String, Object> view(String id) throws FrameException {
			View view = new View();
			MapStationType st = this.checkMapStationType(id);
			BeanUtils.copyProperties(st, view);
			if(StringUtils.isNotBlank(view.getIcon())){
				UploadFile uploadFile = dao.queryEntityById(UploadFile.class, view.getIcon());
				if (uploadFile == null){}
				else {
					view.setIconname(uploadFile.getName());
				}
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("view", view);
			return map;
		}

		@Override
		@Transactional(readOnly=false)
		public String modify(View view, Locale locale) throws FrameException {
			MapStationType st = this.checkMapStationType(view.getId());
			//type_id不能重复
			DetachedCriteria dc = DetachedCriteria.forClass(MapStationType.class)
					.add(Restrictions.eq("typeId", view.getTypeId()))
					.add(Restrictions.ne("typeId", st.getTypeId()))
					.add(Restrictions.eq("flag", Constants.VALID));
			if (dao.queryCount(dc) > 0) {
				throw new FrameException(super.message.getMessage(
			                  "站点类型重复", locale));
				}
			st.setTypeId(view.getTypeId());
			st.setName(view.getName());
			st.setNameEn(view.getNameEn());
			st.setIcon(view.getIcon());
			st.setIndexOrder(view.getIndexOrder());
			st.setRemark(view.getRemark());
			return MessageConstants.MODIFY_SUCCESS;
		}

		@Override
		@Transactional(readOnly=false)
		public FileView stationTypeUploadIcon(MultipartFile file) throws Exception {
			String dirPath = "/stationType/";
			String type="";
			if(file.getOriginalFilename().split(".").length>1){
				type=file.getOriginalFilename().split(".")[1];
			}
			String id = saveFile(file, dirPath, type);
			FileView fileView = new FileView();
			UploadFile uploadFiles = dao.queryEntityById(UploadFile.class, id);
			BeanUtils.copyProperties(uploadFiles, fileView);
			return fileView;
		}


}
